package com.example.kugouwty;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.kugouwty.fragment.DianTaiFragment;
import com.example.kugouwty.fragment.ShiPinFragment;
import com.example.kugouwty.fragment.TuiJianFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    TabLayout faXianTabLayout;
    ViewPager faXianViewPager;
    List<Fragment> fragments;
    String[] faXianTitle = {"推荐", "视频", "电台·听书"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        faXianTabLayout = findViewById(R.id.faXianTableLayout);
        faXianViewPager = findViewById(R.id.faXianViewPager);
        initFragment(); // 初始化Fragment，将Fragment全部填充到fragments中
        faXianViewPager.setAdapter(new myAdapter(getSupportFragmentManager(), fragments)); // 括号中存放PagerAdapter数据类型，需要定义一个类继承PagerAdapter数据类型
        faXianTabLayout.setupWithViewPager(faXianViewPager); // 关联 TabLayout 和 ViewPager

    }

    /**
     * 初始化Fragment
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new TuiJianFragment());
        fragments.add(new ShiPinFragment());
        fragments.add(new DianTaiFragment());
    }

    /**
     * 定义一个类，继承PagerAdapter数据类型
     */
    private class myAdapter extends FragmentPagerAdapter {
        private List<Fragment> list;

        public myAdapter(@NonNull FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position); // 获取具体列表项
        }

        @Override
        public int getCount() {
            return list.size();
        }

        /**
         * 关联内容和标题
         *
         * @param position
         * @return
         */
        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return faXianTitle[position];
        }
    }
}